package cn.wit.front.service.impl;

import cn.wit.front.condition.SearchRecruitInfoCondition;
import cn.wit.front.mapper.CompanyMapper;
import cn.wit.front.mapper.JobCategoryMapper;
import cn.wit.front.mapper.RecruitInfoMapper;
import cn.wit.front.service.RecruitInfoService;
import cn.wit.front.vo.RecruitInfoVO;
import cn.wit.utils.pojo.Company;
import cn.wit.utils.pojo.JobCategory;
import cn.wit.utils.pojo.RecruitInfo;
import cn.wit.utils.response.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RecruitInfoServiceImpl implements RecruitInfoService {
    @Autowired
    private RecruitInfoMapper recruitInfoMapper;
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private JobCategoryMapper jobCategoryMapper;


    @Override
    public Result searchPage(IPage<RecruitInfo> page, SearchRecruitInfoCondition condition) {
        QueryWrapper<RecruitInfo> wrapper3 = new QueryWrapper();
        if (condition != null) {
            if (condition.getJob() != null && condition.getJob() != "") {
                QueryWrapper<JobCategory> wrapper = new QueryWrapper<>();
                wrapper.eq("job_name", condition.getJob());
                JobCategory jobCategory = jobCategoryMapper.selectOne(wrapper);
                if (jobCategory != null) {
                    wrapper3.eq("job_ID", jobCategory.getJobId());
                }else {
                    wrapper3.eq("job_ID", 0);
                }
            }
            if (condition.getCompanyName() != null && condition.getCompanyName() != "") {
                QueryWrapper<Company> wrapper2 = new QueryWrapper<>();
                wrapper2.eq("name", condition.getCompanyName());
                Company company = companyMapper.selectOne(wrapper2);
                if (company != null) {
                    wrapper3.eq("company_ID", company.getCompanyId());
                } else{
                    wrapper3.eq("company_ID", 0);
                }
            }
            if (condition.getWorkPlace() != null && condition.getWorkPlace() != "") {
                wrapper3.eq("work_place", condition.getWorkPlace());
            }
            if (condition.getPay() != null && condition.getPay() != "") {
                wrapper3.gt("pay", condition.getPay());
            }
        }

        IPage<RecruitInfo> recruitInfoIPage = recruitInfoMapper.selectPage(page, wrapper3);
        List<RecruitInfoVO> list = new ArrayList<>();
        for (RecruitInfo info : recruitInfoIPage.getRecords()) {
            Company company = companyMapper.selectById(info.getCompanyId());
            JobCategory job = jobCategoryMapper.selectById(info.getJobId());
            RecruitInfoVO vo = new RecruitInfoVO();
            BeanUtils.copyProperties(info, vo);
            vo.setCompanyName(company.getName());
            vo.setJobName(job.getJobName());
            list.add(vo);
        }
        Page<RecruitInfoVO> voPage = new Page<>(recruitInfoIPage.getCurrent(), recruitInfoIPage.getSize(), recruitInfoIPage.getTotal(), true);
        voPage.setRecords(list);
        return Result.ok(voPage);
    }
}
